﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using System.Data;

namespace WindowsGame1
{
    class parseCSV
    {
        #region private variables
            private StreamReader myReader;
            
            /// <summary>
            /// Header Data: W: dummy, Z: timesteps, X: x-dimension, Y: y-dimension
            /// </summary>
            private Vector4 _headerData;

            private float[,,] _DataArray;
        #endregion

        #region constructors

        public parseCSV(string path)
        {
            string[] temp;
            Char[] charTemp = {' ', ','};
            

            myReader = File.OpenText(path);

            _headerData.W =  Convert.ToInt32(myReader.ReadLine().Trim());

            temp = myReader.ReadLine().Split(charTemp, StringSplitOptions.RemoveEmptyEntries);
            _headerData.Z = Convert.ToInt32(temp[0].Trim());
            _headerData.X = Convert.ToInt32(temp[1].Trim());
            _headerData.Y = Convert.ToInt32(temp[2].Trim());


            temp = myReader.ReadToEnd().Split(charTemp, StringSplitOptions.RemoveEmptyEntries);
    
            _DataArray = new float[(int)_headerData.Z, (int)_headerData.X, (int)_headerData.Y];


            // fill our 3D - Data Array
            int t = 0;
            int x = 0;
            int y = 0;
            foreach (string value in temp)
            {
                if (value.Contains("\r\n"))
                {
                }
                else
                {

                    _DataArray[t, x, y] = float.Parse(value, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.CreateSpecificCulture("en-us"));//(float)Convert.ToDouble(value);
                    y = (y + 1) % (int)_headerData.Y;
                    if (y == 0)
                    {
                        x = (x + 1) % (int)_headerData.X;
                        if (x == 0)
                        {
                            t = (t + 1) % (int)_headerData.Z;
                        }
                    }
                    
                }
            }

        }
        #endregion

        #region public members
        #endregion

        #region properties

        /// <summary>
        /// gets or sets the Headerdata as Vector4
        /// </summary>
        public Vector4 HeaderData
        {
            get
            {
                return _headerData;
            }
            set
            {
                _headerData = value;
            }
        }

        public float[,,] DataArray
        {
            get
            {
                return _DataArray;
            }
            set
            {
                _DataArray = value;
            }
        }
        #endregion
    }

}
